class MyBlockingQueue{
    private String[] data=null;
    public MyBlockingQueue(int capacity){
        data=new String[capacity];
    }
    private int head=0;
    private int tail=0;
    private int size=0;

    public  void put(String s) throws InterruptedException {
        synchronized (this){
            if(size==data.length){
                //  return ;
                this.wait();
            }
            data[tail]=s;
            tail++;
            if(tail>=data.length){
                tail=0;;
            }
            size++;
            this.notify();
        }

    }

    public String take() throws InterruptedException {
        String ret="";
        synchronized (this){
            if(size==0){
               // return null;
                this.wait();
            }
            ret=data[head];
            head++;
            if(head>data.length){
                head=0;
            }
            size--;
            this.notify();
        }

        return ret;
    }

}



public class Demo {
    public static void main(String[] args) {
        MyBlockingQueue queue=new MyBlockingQueue(1000);
        Thread t1=new Thread(()->{
            int i=0;
            //生产者线程
            while(true){
                try {
                    queue.  put(""+i);
                    System.out.println("成功生产元素"+i);
                    i++;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread t2=new Thread(()->{
            //消费者线程
            while (true){
                try {
                    String i= queue.take();
                    System.out.println("消费元素"+i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();
        t2.start();
    }


}
